/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.bytedance.scene.navigation;

import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;
import ohos.agp.window.dialog.ToastDialog;
import ohos.bundle.IBundleManager;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

/**
 * 空页面，为了使用onAbilityResult
 *
 * @since 2021-06-16
 */
public class EmptyAbility extends Ability {
    private static final String WRITE_USER_STORAGE = "ohos.permission.WRITE_USER_STORAGE";
    private static final int REQUEST_CODE_12345 = 12345;
    private static final int REQUEST_CODE_123 = 123;
    private static final int REQUEST_CODE_5 = 5;
    private static final int DURATION = 2000;

    /**
     * onStart
     *
     * @param intent
     */
    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_ability_empty);

        int requestCode = intent.getIntParam("requestCode", 0);
        if (requestCode == REQUEST_CODE_12345) {
            Intent requestIntent = intent.getSequenceableParam("requestIntent");

            // 为什么传这个，在requestIntent取不type
            String type = intent.getStringParam("type");
            requestIntent.setType(type);

            startAbilityForResult(requestIntent, requestCode);
        } else if (requestCode == REQUEST_CODE_123) {
            requestPermission();
        } else if (requestCode == REQUEST_CODE_5) {
            Intent requestIntent = intent.getSequenceableParam("requestIntent");

            // 为什么传这个，在requestIntent取不type
            String type = intent.getStringParam("type");
            requestIntent.setType(type);
        }
    }

    /**
     * 请求权限
     */
    private void requestPermission() {
        if (verifyCallingPermission(WRITE_USER_STORAGE) != IBundleManager.PERMISSION_GRANTED) {
            if (canRequestPermission(WRITE_USER_STORAGE)) {
                requestPermissionsFromUser(new String[]{WRITE_USER_STORAGE}, 0);
            } else {
                if (verifySelfPermission(WRITE_USER_STORAGE) != IBundleManager.PERMISSION_GRANTED){
                    showToast("失败");
                }else {
                    showToast("成功");
                    terminateAbility();
                }
            }
        } else {
            showToast("失败");
        }
    }

    /**
     * onAbilityResult
     *
     * @param requestCode
     * @param resultCode
     * @param resultData
     */
    @Override
    protected void onAbilityResult(int requestCode, int resultCode, Intent resultData) {
        super.onAbilityResult(requestCode, resultCode, resultData);
        if (NavigationScene.mInstance != null) {
            NavigationScene.mInstance.onAbilityResult(requestCode, resultCode, resultData);
        }
        terminateAbility();
    }

    /**
     * onRequestPermissionsFromUserResult
     *
     * @param requestCode
     * @param permissions
     * @param grantResults
     */
    @Override
    public void onRequestPermissionsFromUserResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsFromUserResult(requestCode, permissions, grantResults);
        if (NavigationScene.mInstance != null) {
            NavigationScene.mInstance.onRequestPermissionsFromUserResult(requestCode, permissions, grantResults);
        }
        terminateAbility();
    }

    /**
     * Toast弹框
     *
     * @param content 内容
     */
    protected void showToast(String content) {
        Component toastLayout = LayoutScatter.getInstance(this)
                .parse(com.bytedance.scene.ResourceTable.Layout_layout_toast, null, false);
        Text toastText = (Text) toastLayout.findComponentById(com.bytedance.scene.ResourceTable.Id_text_msg_toast);
        toastText.setText(content);
        new ToastDialog(this)
                .setComponent(toastLayout)
                .setTransparent(true)
                .show();
    }
}
